/*
 * Created on 06-Sep-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package wstrader.task.quality;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pc.sql.value.*;

import pc.system.Tasks;
import wstrader.task.trade.TradeTask;

/**
 * @author NANSA
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SampleTask extends TradeTask
{

	public SampleTask(Tasks owner, String menu)
	  {
	    super(owner,menu);
	  }	
	
	  public SampleTask(Tasks owner)
	  {
	    super(owner);
	  }

	  public String getCategory(int id)
	  {
	  	String Category = "-";
	  	if (id > 0)
	  	{	  	
	  		if (id == 1) Category = "Approval";
	  		if (id == 2) Category = "Pre-Ship/Delvy";
	  		if (id == 3) Category = "Ship/Delvy";
	  		if (id == 4) Category = "Stock";
	  		if (id == 5) Category = "Ref/Type/Offer";
	  	}
	  	else
	  	{
	  		Category="-";
	  	}
	  	return Category;
	  }
      
      public ResultSet getQualityRefList(String Type)
      {
      	
      	String Sql="";
     	Sql ="select sample_dts_id,ref_number from quality_report where type = '" + Type + "' and sample_dts_id > 0";
  	  	return getSQLCommand().executeQuery(Sql);        	
      } 
      
      
      public ResultSet getSampleRefList(String Type)
      {
      	String Sql="";
     	Sql ="select id,ref_number from sample_master where sample_type = '" + Type + "'";
  	  	return getSQLCommand().executeQuery(Sql);        	
      }
      
      public long getQualityId(long id)
      {
      	String Sql="";
      	long Qid =0;
     	Sql ="select id from quality_report where sample_dts_id = " + id;
  	  	ResultSet Rs = getSQLCommand().executeQuery(Sql);
  	  	try {
			Rs.beforeFirst();
			if (Rs.next()==true) Qid = Rs.getLong("id");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  	  	
		return Qid;
      }  
      
      public ResultSet getSampleDateList()
      {
      	String SType = getRequest().getStringValue("sample_type");
      	if (SType==null) SType = "0";
      	String Sql="";
      	if (SType.compareTo("0")==0) Sql ="select sent_received_date from sample_master";
      	if (SType.compareTo("S")==0) Sql ="select sent_received_date from sample_master where sample_type = 'S'";
      	if (SType.compareTo("R")==0) Sql ="select sent_received_date from sample_master where sample_type = 'R'";      	

  	  	return getSQLCommand().executeQuery(Sql);        	
      }     
      
      public ResultSet getSampleBuyerSellerList()
      {
      	String SType = getRequest().getStringValue("sample_type");
      	if (SType==null) SType = "0";
      	String Sql="";
      	if (SType.compareTo("0")==0) Sql ="select id,name from company_master where seller = 1 or buyer = 1";
      	if (SType.compareTo("S")==0) Sql ="select id,name from company_master where seller = 1";
      	if (SType.compareTo("R")==0) Sql ="select id,name from company_master where buyer = 1";      	

  	  	return getSQLCommand().executeQuery(Sql);        	
      }  
      
      public ResultSet fetchSampleTypeList(String QType)
      {
      	String SType = getRequest().getStringValue("sample_type");
      	if (SType==null) SType = "0";
	  	java.sql.Date SentDate = getRequest().getValueDate("sent_received_date");
	  	int SentTo = getRequest().getIntValue("buyer_seller_id");
	  	int OriginId = getRequest().getIntValue("origin_id");
	 	int GradeId = getRequest().getIntValue("grade_id");
	 	
      	String Sql="";
      		Sql ="SELECT quality_report.id,quality_report.`type`,quality_report.ref_number, " +
			 "quality_report.above_sc20,quality_report.sc20,quality_report.sc19, " +
			 "quality_report.sc18,quality_report.sc17,quality_report.sc16, " +
			 "quality_report.sc15,quality_report.sc14,quality_report.sc13, " +
			 "quality_report.sc12,quality_report.below_sc12,quality_report.black, " +
			 "quality_report.broken,quality_report.black_broken,quality_report.brown, " +
			 "quality_report.moisture,quality_report.old_crop,quality_report.foreign_matter, " +
			 "quality_report.worm,quality_report.moldy,quality_report.remark, " +
			 "quality_report.user_id,quality_report.sample_dts_id,sample_details.location, " +
			 "sample_details.quality_id,sample_details.origin_id,sample_details.grade_id,sample_details.approved_reject, " +
			 "sample_master.buyer_seller_id, " +
			 "sample_master.sample_type " +
			 "FROM quality_report " +
			 "INNER JOIN sample_details ON (quality_report.sample_dts_id=sample_details.id) " +
			 "INNER JOIN sample_master ON (sample_details.sample_master_id=sample_master.id) " +
			 "where (sample_master.sent_received_date = ? OR ?) " +
			 "and (sample_master.buyer_seller_id = ? OR ?) " +
			 "and (sample_master.sample_type = ? OR ?) " +
			 "and (sample_details.origin_id = ? OR ?) " +
			 "and (sample_details.grade_id = ? OR ?) " +
			 "and (quality_report.type= '" + QType + "')";
      		
      		Vector vp = new Vector();

      		vp.addElement(new DateValue(SentDate));
      		vp.addElement(new BooleanValue(SentDate.compareTo(new java.sql.Date(0)) == 0));

      		vp.addElement(new IntValue(SentTo));
      		vp.addElement(new BooleanValue(SentTo == 0));

      		vp.addElement(new StringValue(SType));
      		vp.addElement(new BooleanValue(SType.compareTo("0") == 0));

      		vp.addElement(new IntValue(OriginId));
      		vp.addElement(new BooleanValue(OriginId == 0));

      		vp.addElement(new IntValue(GradeId));
      		vp.addElement(new BooleanValue(GradeId == 0));	  		
      		
      		//vp.addElement(new StringValue("S"));
      		
      		return getSQLCommand().executeQuery(Sql,vp);	  	
        	
      }      
}

